package org.dromara.fms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.fms.domain.bo.FmsBillTypeBo;
import org.dromara.fms.domain.vo.FmsBillTypeVo;
import org.dromara.fms.domain.FmsBillType;
import org.dromara.fms.mapper.FmsBillTypeMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 财务主Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class FmsBillTypeServiceImpl extends BaseService {

    private final FmsBillTypeMapper baseMapper;

    /**
     * 查询财务主
     */
    public FmsBillTypeVo queryById(Long typeId){
        return baseMapper.selectVoById(typeId);
    }

    /**
     * 查询财务主列表
     */
    public TableDataInfo<FmsBillTypeVo> queryPageList(FmsBillTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FmsBillType> lqw = buildQueryWrapper(bo);
        Page<FmsBillTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询财务主列表
     */
    public List<FmsBillTypeVo> queryList(FmsBillTypeBo bo) {
        LambdaQueryWrapper<FmsBillType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FmsBillType> buildQueryWrapper(FmsBillTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FmsBillType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), FmsBillType::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getFmsType()), FmsBillType::getFmsType, bo.getFmsType());
        return lqw;
    }

    /**
     * 新增财务主
     */
    public Boolean insertByBo(FmsBillTypeBo bo) {
        FmsBillType add = MapstructUtils.convert(bo, FmsBillType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改财务主
     */
    public Boolean updateByBo(FmsBillTypeBo bo) {
        FmsBillType update = MapstructUtils.convert(bo, FmsBillType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FmsBillType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除财务主
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
